package com.ruoyi.project.im.groups.controller;

import java.util.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.RandomUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.im.doyuming.domain.Doyuming;
import com.ruoyi.project.im.doyuming.service.IDoyumingService;
import com.ruoyi.project.im.personhost.domain.Personhost;
import com.ruoyi.project.im.personhost.service.IPersonhostService;
import com.ruoyi.project.im.utils.Http;
import com.ruoyi.project.im.utils.RandomNum;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.im.groups.domain.Groups;
import com.ruoyi.project.im.groups.service.IGroupsService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

import javax.websocket.server.PathParam;

/**
 * 群信息Controller
 * 
 * @author ruoyi
 * @date 2022-11-10
 */
@Controller
@RequestMapping("/im/groups")
public class GroupsController extends BaseController
{
    private String prefix = "im/groups";


    @Autowired
    private IGroupsService groupsService;

    @Autowired
    private IPersonhostService iPersonhostService;

    @Autowired
    private IDoyumingService iDoyumingService;

    @RequiresPermissions("im:groups:view")
    @GetMapping()
    public String groups(String creatorUserId,ModelMap map)
    {
        map.put("kefuId",creatorUserId);
        return prefix + "/groups";
    }

    /**
     * 查询群信息列表
     */
    @RequiresPermissions("im:groups:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Groups groups)
    {
        startPage();
        List<Groups> list = groupsService.selectGroupsList(groups);
        return getDataTable(list);
    }

    /**
     * 导出群信息列表
     */
    @RequiresPermissions("im:groups:export")
    @Log(title = "群信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Groups groups)
    {
        List<Groups> list = groupsService.selectGroupsList(groups);
        ExcelUtil<Groups> util = new ExcelUtil<Groups>(Groups.class);
        return util.exportExcel(list, "群信息数据");
    }

    /**
     * 新增群信息
     */
    @GetMapping("/add")
    public String add(String creatorUserId , ModelMap map)
    {
        map.put("creatorUserId",creatorUserId);
        return prefix + "/add";
    }


    /**
     * 新增保存群信息
     */
    @RequiresPermissions("im:groups:add")
    @Log(title = "群信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Groups groups)
    {
        if(StringUtils.isEmpty(groups.getCreatorUserId())){
            return AjaxResult.error("创建人不能为空");
        }

//        groups.setCreatorUserId();
        return toAjax(groupsService.insertGroups(groups));
    }

    /**
     * 修改群信息
     */
    @RequiresPermissions("im:groups:edit")
    @GetMapping("/edit/{groupId}")
    public String edit(@PathVariable("groupId") String groupId, ModelMap mmap)
    {
        Groups groups = groupsService.selectGroupsByGroupId(groupId);
        mmap.put("groups", groups);
        return prefix + "/edit";
    }

    /**
     * 修改保存群信息
     */
    @RequiresPermissions("im:groups:edit")
    @Log(title = "群信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Groups groups)
    {
        return toAjax(groupsService.updateGroups(groups));
    }

    /**
     * 删除群信息
     */
    @RequiresPermissions("im:groups:remove")
    @Log(title = "群信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(groupsService.deleteGroupsByGroupIds(ids));
    }
}
